<?php

/**
 * This is the model class for table "{{area}}".
 *
 * The followings are the available columns in table '{{area}}':
 * @property integer $id
 * @property integer $pid
 * @property string $areaname
 * @property string $postcode
 * @property string $cnode
 * @property string $pnode
 * @property integer $level
 * @property integer $createtime
 * @property integer $updatetime
 * @property string $isactive
 */
class Area extends CDbActiveRecord {
	public $alias = 'area';
	public $children = '';
	public static function model($className = __CLASS__) {
		return parent::model ( $className );
	}
	public function tableName() {
		return 'webapp.wdb_area';
	}
	public function rules() {
		return array (
				array (
						'pid, areaname, createtime, updatetime',
						'required' 
				),
				array (
						'pid, level, createtime, updatetime',
						'numerical',
						'integerOnly' => true 
				),
				array (
						'areaname',
						'length',
						'max' => 20 
				),
				array (
						'postcode',
						'length',
						'max' => 6 
				),
				array (
						'cnode, pnode',
						'length',
						'max' => 50 
				),
				array (
						'sort',
						'length',
						'max' => 4 
				),
				array (
						'isactive',
						'length',
						'max' => 1 
				),
				array (
						'id, pid, areaname, postcode, cnode, pnode, level, sort, createtime, updatetime, isactive',
						'safe',
						'on' => 'search' 
				) 
		);
	}
	public function relations() {
		return array ();
	}
	public function attributeLabels() {
		return array (
				'id' => 'ID',
				'pid' => '父ID',
				'areaname' => '名称',
				'postcode' => '邮编',
				'cnode' => 'Cnode',
				'pnode' => 'Pnode',
				'level' => '级数',
				'sort' => '排序',
				'createtime' => 'Createtime',
				'updatetime' => 'Updatetime',
				'isactive' => 'Isactive' 
		);
	}
	public function search() {
		$criteria = new CDbCriteria ();
		$criteria->compare ( 'id', $this->id );
		// 所有父节点
		// $criteria->compare ( 'pid', 0 );
		$criteria->compare ( 'areaname', $this->areaname, true );
		$criteria->compare ( 'postcode', $this->postcode, true );
		$criteria->compare ( 'cnode', $this->cnode, true );
		$criteria->compare ( 'pnode', $this->pnode, true );
		$criteria->compare ( 'level', $this->level );
		$criteria->compare ( 'createtime', $this->createtime );
		$criteria->compare ( 'updatetime', $this->updatetime );
		$criteria->compare ( 'isactive', $this->isactive, true );
		
		/*
		 * return new CActiveDataProvider($this, array( 'criteria'=>$criteria,
		 * ));
		 */
		$setting = array (
				'id' => 'id',
				'parentid' => 'pid',
				'name'=>'name',
				'sort' => 'sort' 
		);
		return new CActiveTreeProvider ( $this, array (
				'criteria' => $criteria 
		), $setting );
	}
}